﻿using Autodesk.AutoCAD.Runtime;
using Autodesk.Windows;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;

namespace AutoCADPlugin
{
    /// <summary>
    /// 窗口界面
    /// </summary>
    public  class Ribbon
    {


        /// <summary>
        /// 添加界面
        /// </summary>
        [CommandMethod("AddRibbon")]

        //层级关系：Table桌面-->Panel控制板-->Button按钮
        public static void AddRibbon()
        {
            RibbonControl rc = ComponentManager.Ribbon;//获取当前Ribbon
            //检查所有面板是否有我们要找的table
            string tabId = "MyTool";
            var tabs = rc.Tabs.Select(d => d.Id).ToList();//获得指定对象的集合
            if (tabs.Contains(tabId))
            {
                var tab = rc.Tabs.FirstOrDefault(d => d.Id == tabId);//找到第一个满足条件的对象
                rc.Tabs.Remove(tab);
            }
            //foreach (var tab in rc.Tabs)
            //{
            //    if (tab.Id==tabId)
            //    {
            //        return;
            //    }
            //}
            try
            {
                RibbonTab rt = new RibbonTab
                {
                    Title = "我的工具",
                    Id = tabId,
                    IsActive = true
                };
                rc.Tabs.Add(rt);

                RibbonPanel rp = new RibbonPanel();
                rt.Panels.Add(rp);//在主界面上添加控制板

                RibbonPanelSource rps = new RibbonPanelSource();
                rps.Title = "车位工具";
                rp.Source = rps;

                string btnName = "添加车位";

                RibbonButton rb = new RibbonButton()
                {
                    Name = btnName,
                    Text = btnName,
                    ShowText = true,
                    Image = GetBitmapImage(Properties.Resources.AddCar,16),
                    LargeImage= GetBitmapImage(Properties.Resources.AddCar, 32),
                    Orientation=System.Windows.Controls.Orientation.Vertical,
                    CommandHandler=new RibbonCommandHandler(),
                    CommandParameter="AddCar",
                    Size=RibbonItemSize.Large
                };

                //层级关系：Table桌面-->Panel控制板-->Button按钮
                rps.Items.Add(rb);

                RibbonToolTip rtt = new RibbonToolTip
                {
                    Title = "添加车位",
                    Content = "添加车位的功能",
                    Command = "AddCar",
                    ExpandedContent = "添加车位，排序及统计",
                    ExpandedImage = GetBitmapImage(Properties.Resources.AddCarToolTip),
                };
                rb.ToolTip = rtt;
            }
            catch (System.Exception)
            {
                throw;
            }
        }

        static internal BitmapImage GetBitmapImage(Bitmap image, int size = 0)
        {
            BitmapImage bmp = new BitmapImage();
            MemoryStream stream = new MemoryStream();
            if (size > 0)
            {
                using (var newImage = new Bitmap(size, size))
                using (var graphics = Graphics.FromImage(newImage))
                {
                    graphics.SmoothingMode = SmoothingMode.AntiAlias;
                    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    graphics.DrawImage(image, new Rectangle(0, 0, size, size));
                    newImage.Save(stream, ImageFormat.Png);
                }
            }
            else
                image.Save(stream, ImageFormat.Png);
            bmp.BeginInit();
            bmp.StreamSource = stream;
            bmp.EndInit();
            return bmp;
        }
    }
}
